<template>
	<view>
		<u-sticky bgColor="#fff">
			<view class="help-pubuBox">
				<u-search placeholder="搜索备件名称或编号" :showAction="false" height="40" shape="square" color="#2B2B2B" :clearabled="true" v-model="title" @change="change" @search="chaxun"
					@custom="chaxun" ></u-search>
			</view>
		</u-sticky>
		<view class="search-title">
			<view class="text1">全部备件列表</view>
			<view>备件数量：{{List.length}}件</view>
		</view>
		<view class="search-list">
			<view class="search-item" v-for="(item,index) in List" :key="item.partId">
				<view class="item-top">
					<view class="iconfont " @click.native="selectItem(item)"
						:class="[item.checked?'icon-app-duoxuanyixuan select-bg':'icon-app-duoxuanweixuan unselect-bg']">
					</view>
					<!-- <view class="item-name"> {{item.name}}</view> -->
					<view class="item-txt">
						<view class="left-icon">
							<!-- <image
								:src=" item.picture!=''?item.picture:'https://img.js.design/assets/smartFill/img429164da758808.jpg'">
							</image> -->
							<image :src="item.picture?item.picture:'../../../static/images/index/beijian.png'">
							</image>
						</view>
						<view class="right-txt">
							<view class="txt1">{{item.partNum!=''?item.partNum+'-':''}}{{item.partName}}</view>
							<!-- <view class="txt1">{{item.sbmodel}}</view> -->
							<view class="txt-right">
								<view class="txt2">{{item.partTypeName}}</view>
								<view class="addnum">
									<u-number-box v-model="item.bjNum" @change="onChange" :name="index"
										:disablePlus="item.bjNum>=item.realStock" :max="item.realStock">
									</u-number-box>
								</view>
							</view>

						</view>
					</view>
				</view>

			</view>
		</view>
		<view class="empty-data" v-if="List.length==0">
			<u-empty mode="data" :marginTop="100" text="暂无搜索内容"></u-empty>
		</view>
		<!-- <u-loadmore :status="status" :line="true" /> -->
		<view class="clear-bottom"></view>
		<view class="btn-bottom">
			<view class="text-left">
				已选备件总数量： <text class="textColor">{{bjTotal}}</text> 件
			</view>
			<view class="btn-right"  @click.native.stop="$dianji(confirmChange,'onoff') ">
				确定
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: 'loadmore',
				title: "",
				// actionStyle: {
				// 	color: '#fff',
				// 	background: '#0F53AC',
				// 	padding: '10rpx',
				// 	borderRadius: '8rpx'
				// },
				page: 1,
				comList: [],
				List: [],
				value: 1,
				onoff: true,
				params: {
					pn: 1,
					ps: 10,
					deviceType: '', //必须 设备类型id
					partType: '', //非必须 备件类型id
					partType: '', //非必须 备件类型id
					keyword: '',
					inventoryFlag: 1 //是否查询有库存的，1是
				},
				bjTotal: 0,
				disablePlus: false,
				dataParams: {
					orderNum: '', //非必须 维修工单号
					approveStatus: '', //非必须 审核状态；0.待提交  1.待审核    2.已审核
					deviceId: '', //非必须 申请的设备id
					partApplyDetails: [], //非必须 维修工单号
					applyType: ''
				},

				isConfirm: false,
				pageType: '', //1-委外维修 2-内部维修 
				repairOrderNum: '',
				backData: {},
				bjType: '',
				bjapplyList: []
			}
		},
		onLoad(option) {
			if (option) {
				this.params.deviceType = option.deviceType;
				this.pageType = option.pageType;
				if (option.bjapplyList) {
					this.bjapplyList = JSON.parse(decodeURIComponent(option.bjapplyList)).filter(item => item
						.approveStatus == 0)
				}
				if (option.deviceId){
					this.dataParams.deviceId = option.deviceId
				}

				if (option.repairOrderNum == 'undefined') {
					this.dataParams.orderNum = '';
				} else {
					this.dataParams.orderNum = option.repairOrderNum
				}
				this.bjType = option.bjType;
			}
			this.getList();
		},
		onShow() {
			this.isConfirm = false
		},
		methods: {
			change(v) {
				this.title = v
				this.params.pn = 1;
				this.params.keyword = this.title;
				this.getList();
			},
			chaxun() {
				this.params.pn = 1;
				this.params.keyword = this.title;
				this.getList();
			},
			selectItem(item) {
				item.checked = !item.checked;
				this.getbjTotal();
			},
			getList() {
				const that=this
				uni.$u.http.post(`/device/mobile/devicePartRelevance/partRelevanceListAll`, that.params).then(res => {
					console.log('res===>getList', res)
					if (res.code == 0) {
						let obj1 = res.data.records;
						obj1.forEach(item => {
							item.bjNum = 1;
						})
						obj1.forEach(item=>{
							that.bjapplyList.forEach(i=>{
								if(item.partId==i.partId){
									item.bjNum=i.totalAmount
								}
							})
						})
						that.List = JSON.parse(JSON.stringify(obj1))
					}
				}).catch(err => {})
			},
			onChange(e) {
				this.List[e.name].bjNum = e.value;
				if (this.List[e.name].bjNum > this.List[e.name].realStock) {
					// uni.$u.toast('不能超过最大库存')
					// this.List[e.name].bjNum = this.List[e.name].realStock;
					// this.getbjTotal();
					// return false
				}
				this.getbjTotal();
			},
			/**
			 * 获取已选备件总数量
			 */
			getbjTotal() {
				this.bjTotal = 0;
				this.dataParams.partApplyDetails = []
				this.comList = []
				this.List.forEach(item => {
					if (item.checked) {
						this.bjTotal = this.bjTotal + item.bjNum;
						let dd = {
							partId: item.partId,
							amount: item.bjNum
						}
						this.dataParams.partApplyDetails.push(dd)
						this.comList.push(item)
					}
				})
			},
			//确定按钮
			confirmChange() {
				if (this.dataParams.partApplyDetails.length == 0) return uni.$u.toast('请选择备件')
				this.dataParams.approveStatus = 0;
				if (this.bjType == 1) {
					let updateParams = {
						orderNum: this.dataParams.orderNum,
						detailResponses: []
					}
					if (this.bjapplyList.length > 0) {
						this.bjapplyList.forEach(item => {
							this.dataParams.partApplyDetails.forEach(i => {
								if (i.partId == item.partId) {
									let obj = {
										partId: i.partId,
										applyDetailId: item.id, //详情id
										amount: i.amount //数量
									}
									updateParams.detailResponses.push(obj)
								}
							})
						})
					}
					if (updateParams.detailResponses.length > 0) {
						this.dataParams.partApplyDetails = this.dataParams.partApplyDetails.filter(item => {
							let idList = updateParams.detailResponses.map(v => v.partId)
							console.log(idList, 'idList')
							console.log('item', item)
							return idList.indexOf(item.partId) == -1
						})
					}
					if (updateParams.detailResponses.length > 0) {
						updateParams.detailResponses.forEach(item => {
							delete item.partId
						})
						//更新
						uni.$u.http.post(`/device/mobile/devicePartApply/updatePartApplyDetail`, updateParams).then(
							res => {
								if (res.code == 0) {
									this.backData = res.data;
									let data = {
										orderNum: this.dataParams.orderNum,
										partUsageResponse: res.data
									}

									if (this.dataParams.partApplyDetails.length > 0) {
										uni.$u.http.post(`/device/mobile/deviceRepair/addRepairPartApply`, this
											.dataParams).then(
											res => {
												if (res.code == 0) {
													uni.$u.toast('添加成功')
													this.backData = res.data;
												}
												setTimeout(() => {
													uni.$emit('comList', this.backData);
													uni.$emit('comList2', this.backData);
													uni.navigateBack({
														delta: 1 //返回层数，2则上上页
													});
												}, 500)
											}).catch(err => {
											uni.$u.toast(err)
										})
									} else {
										uni.$u.toast('添加成功')
										setTimeout(() => {
											uni.$emit('comList', data);
											uni.$emit('comList2', data);
											uni.navigateBack({
												delta: 1 //返回层数，2则上上页
											});
										}, 500)
									}
								}
							}).catch(err => {
							uni.$u.toast(err)
						})
					} else if (this.dataParams.partApplyDetails.length > 0) {
						uni.$u.http.post(`/device/mobile/deviceRepair/addRepairPartApply`, this.dataParams).then(
							res => {
								console.log('res===>添加备件申请', res)
								if (res.code == 0) {
									uni.$u.toast('添加成功')
									this.backData = res.data;
								}
							}).catch(err => {})
						setTimeout(() => {
							uni.$emit('comList', this.backData);
							uni.$emit('comList2', this.backData);
							uni.navigateBack({
								delta: 1 //返回层数，2则上上页
							});
						}, 500)
					}
					// uni.$u.http.post(`/device/mobile/deviceRepair/addRepairPartApply`, this.dataParams).then(
					// 	res => {
					// 		console.log('res===>添加备件申请', res)
					// 		if (res.code == 0) {
					// 			uni.$u.toast('添加成功')
					// 			this.backData = res.data;
					// 		}
					// 	}).catch(err => {})
					// setTimeout(() => {
					// 	uni.$emit('comList', this.backData);
					// 	uni.$emit('comList2', this.backData);
					// 	uni.navigateBack({
					// 		delta: 1 //返回层数，2则上上页
					// 	});
					// }, 500)
				}
				if (this.bjType == 2) {
					this.dataParams.applyType = 2;
					let updateParams = {
						orderNum: this.dataParams.orderNum,
						detailResponses: []
					}
					if (this.bjapplyList.length > 0) {
						this.bjapplyList.forEach(item => {
							this.dataParams.partApplyDetails.forEach(i => {
								if (i.partId == item.partId) {
									let obj = {
										partId: i.partId,
										applyDetailId: item.id, //详情id
										amount: i.amount //数量
									}
									updateParams.detailResponses.push(obj)
								}
							})
						})
					}
					if (updateParams.detailResponses.length > 0) {
						this.dataParams.partApplyDetails = this.dataParams.partApplyDetails.filter(item => {
							let idList = updateParams.detailResponses.map(v => v.partId)
							return idList.indexOf(item.partId) == -1
						})
					}
					if (updateParams.detailResponses.length > 0) {
						updateParams.detailResponses.forEach(item => {
							delete item.partId
						})
						//更新
						uni.$u.http.post(`/device/mobile/devicePartApply/updatePartApplyDetail`, updateParams).then(
							res => {
								if (res.code == 0) {
									if (this.dataParams.partApplyDetails.length > 0) {
										uni.$u.http.post(`/device/mobile/devicePartApply/savePartApply`, this
											.dataParams).then(
											res => {
												if (res.code == 0) {
													uni.$u.toast('添加成功')
													this.backData = res.data;
												}
												setTimeout(() => {
													uni.$emit('comList3', this.backData); //维保
													// uni.$emit('comList4',this.backData); //点检
													uni.navigateBack({
														delta: 1 //返回层数，2则上上页
													});
												}, 500)
											}).catch(err => {
											uni.$u.toast(err)
										})
									} else {
										this.backData = res.data;
										uni.$u.toast('添加成功')
										setTimeout(() => {
											uni.$emit('comList3', this.backData); //维保
											// uni.$emit('comList4',this.backData); //点检
											uni.navigateBack({
												delta: 1 //返回层数，2则上上页
											});
										}, 500)
									}
								}
							}).catch(err => {
							uni.$u.toast(err)
						})
					} else if (this.dataParams.partApplyDetails.length > 0) {
						uni.$u.http.post(`/device/mobile/devicePartApply/savePartApply`, this.dataParams).then(
							res => {
								console.log('res===>维保 添加备件申请', res)
								if (res.code == 0) {
									uni.$u.toast('添加成功')
									this.backData = res.data;
								}
							}).catch(err => {})
						setTimeout(() => {
							uni.$emit('comList3', this.backData); //维保
							// uni.$emit('comList4',this.backData); //点检
							uni.navigateBack({
								delta: 1 //返回层数，2则上上页
							});
						}, 500)
					}
				}
				if (this.bjType == 3) {
					this.dataParams.applyType = 3;
					let updateParams = {
						orderNum: this.dataParams.orderNum,
						detailResponses: []
					}
					if (this.bjapplyList.length > 0) {
						this.bjapplyList.forEach(item => {
							this.dataParams.partApplyDetails.forEach(i => {
								if (i.partId == item.partId) {
									let obj = {
										partId: i.partId,
										applyDetailId: item.id, //详情id
										amount: i.amount //数量
									}
									updateParams.detailResponses.push(obj)
								}
							})
						})
					}
					if (updateParams.detailResponses.length > 0) {
						this.dataParams.partApplyDetails = this.dataParams.partApplyDetails.filter(item => {
							let idList = updateParams.detailResponses.map(v => v.partId)
							return idList.indexOf(item.partId) == -1
						})
					}
					if (updateParams.detailResponses.length > 0) {
						updateParams.detailResponses.forEach(item => {
							delete item.partId
						})
						//更新
						uni.$u.http.post(`/device/mobile/devicePartApply/updatePartApplyDetail`, updateParams).then(
							res => {
								if (res.code == 0) {
									if (this.dataParams.partApplyDetails.length > 0) {
										uni.$u.http.post(`/device/mobile/devicePartApply/savePartApply`, this
											.dataParams).then(
											res => {
												if (res.code == 0) {
													uni.$u.toast('添加成功')
													this.backData = res.data;
												}
												setTimeout(() => {
													uni.$emit('comList4', this.backData); //点检
													uni.navigateBack({
														delta: 1 //返回层数，2则上上页
													});
												}, 500)
											}).catch(err => {
											uni.$u.toast(err)
										})
									} else {
										this.backData = res.data;
										uni.$u.toast('添加成功')
										setTimeout(() => {
											uni.$emit('comList4', this.backData);
											uni.navigateBack({
												delta: 1 //返回层数，2则上上页
											});
										}, 500)
									}
								}
							}).catch(err => {
							uni.$u.toast(err)
						})
					} else if (this.dataParams.partApplyDetails.length > 0) {
						uni.$u.http.post(`/device/mobile/devicePartApply/savePartApply`, this.dataParams).then(
							res => {
								if (res.code == 0) {
									uni.$u.toast('添加成功')
									this.backData = res.data;
								}
							}).catch(err => {})
						setTimeout(() => {
							uni.$emit('comList4', this.backData); //维保
							uni.navigateBack({
								delta: 1 //返回层数，2则上上页
							});
						}, 500)
					}


					// uni.$u.http.post(`/device/mobile/devicePartApply/savePartApply`, this.dataParams).then(
					// 	res => {
					// 		console.log('res===>维保 添加备件申请', res)
					// 		if (res.code == 0) {
					// 			uni.$u.toast('添加成功')
					// 			this.backData = res.data;
					// 		}
					// 	}).catch(err => {})
					// setTimeout(() => {
					// 	// uni.$emit('comList3',this.backData); //维保
					// 	uni.$emit('comList4', this.backData); //点检
					// 	uni.navigateBack({
					// 		delta: 1 //返回层数，2则上上页
					// 	});
					// }, 500)
				}

			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #ffffff;
	}

	.help-pubuBox {
		padding: 28rpx 32rpx;
		background: #fff;
	}

	.search-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 28rpx;
		color: #8c8c8c;
		background-color: #F5F5F5;
		padding: 30rpx;
		
		.text1 {
			color: #4D4D4D;
		}
	}

	.search-list {
		background: #fff;
		margin-top: 2rpx;


		.search-item {
			padding: 30rpx;
			border-bottom: 1px solid #f2f2f2;

			.item-top {
				display: flex;
				align-items: center;


				.unselect-bg {
					margin-right: 20rpx;
					color: #ffffff;
					font-size: 48rpx;
					// border: 1px solid #E0E0E0;
					// border-radius: 12rpx;
					text-shadow: 0 1px #E0E0E0, 1px 0 #E0E0E0, -1px 0 #E0E0E0, 0 -1px #E0E0E0;
				}

				.select-bg {
					margin-right: 20rpx;
					color: #0F53AC;
					font-size: 48rpx;
					// text-shadow: 0 1px #E0E0E0, 1px 0 #E0E0E0, -1px 0 #E0E0E0, 0 -1px #E0E0E0;
				}

				.item-name {
					color: #000000;
					font-size: 34rpx;
				}

				.item-txt {
					padding: 20rpx 0 20rpx 10rpx;
					display: flex;
					width: 100%;

					.left-icon {
						border-radius: 8rpx;

						image {
							width: 152rpx;
							height: 152rpx;
							border-radius: 8rpx;
						}
					}

					.right-txt {
						padding-left: 32rpx;
						width: 100%;

						.txt1 {
							width: 440rpx;
							height: 75rpx;
							font-size: 32rpx;
							color: #2B2B2B;
							padding-bottom: 20rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
						}

						.txt-right {
							display: flex;
							align-items: center;
							justify-content: space-between;
							padding-top: 10rpx;

							.txt2 {
								font-size: 28rpx;
								color: #8c8c8c;
							}

							.addnum {
								.minus {
									color: #0F53AC;
									font-size: 34rpx;
								}

								.plus {
									color: #0F53AC;
									font-size: 34rpx;
								}

								.notselect-bg {
									color: #c4c4c4;
									font-size: 34rpx;
								}
							}
						}
					}
				}

			}


		}
	}

	.clear-bottom {
		width: 100%;
		height: 196rpx;
	}

	.btn-bottom {
		width: 100%;
		// height: 120rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		z-index: 10000;
		bottom: 0;
		background: #fff;
		border-top: 1px solid #F5F5F5;
		padding: 24rpx 0 86rpx 0;

		.text-left {
			margin-left: 32rpx;
			font-size: 28rpx;
			color: #2B2B2B;
			
			.textColor {
				color: #FA4016;
			}
		}

		.btn-right {
			width: 24%;
			height: 80rpx;
			margin-right: 30rpx;
			font-size: 32rpx;
			color: #ffffff;
			background: #0F53AC;
			border-radius: 12rpx;
			// padding: 20rpx 60rpx;
		}

	}
</style>